<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<title>Layui</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="./layui/css/layui.css">
	<link rel="stylesheet" href="./css/index.css">
	<!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
	<style>
		.body,
		html {
			width: 100%;
			height: 100%;
			margin: 0;
			padding: 0;
			background-image: url("image/1.png");
		}

		.main {
			text-align: center;
			background-color: #FFF0F5;
			/*让div内部文字居中*/
			border-radius: 20px;
			width: 500px;
			height: 350px;
			margin: auto;
			position: absolute;
			top: 0;
			left: 0;
			right: 0;
			bottom: 0;
		}

		.input_size {
			margin-right: 15%;
		}
	</style>
	<script>
		window.localStorage.clear();
		function getParams(key) {
			var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
			var r = window.location.search.substr(1).match(reg);
			if (r != null) {
				return unescape(r[2]);
			}
			return null;
		};
		var unlogin = getParams("unlogin");
		if (unlogin != null) {
			alert("您还未登录，请先登录！")
			window.location = "./login.html";
		}
	</script>
</head>

<body>
	<div class="center">
		<form class="layui-form main" action="" lay-filter="example">
			<h1 class="dl" style="margin-bottom: 50px;margin-top: 5%;">后端登录页面</h1>
			<div class="layui-form-item input_size">
				<label class="layui-form-label" style="font-size: 20px;">账号</label>
				<div class="layui-input-block">
					<input type="text" name="username" value="admin1" lay-verify="title" size="20" autocomplete="off"
						placeholder="请输入标题" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item input_size">
				<label class="layui-form-label" style="font-size: 20px;">密码</label>
				<div class="layui-input-block">
					<input type="password" name="password" value="admin" placeholder="请输入密码" autocomplete="off"
						class="layui-input">
				</div>
			</div>
			<div class="layui-form-item input_size">
				<div class="layui-input-block">
					<button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">登录</button>
					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
				</div>
			</div>
		</form>
	</div>

	<script src="./layui/layui.js" charset="utf-8"></script>
	<script src="./js/md5.js"></script>
	<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->
	<script>

		function check_audit() {
			if (localStorage.type < 20) {
				alert("权限不足已记录当前登录ip，请刷新页面后重试");
				localStorage.type = data.type;
				return;
			}
		}

		layui.use(['table'], function () {
			var form = layui.form
				, layer = layui.layer
				, layedit = layui.layedit
				, laydate = layui.laydate
				, $ = layui.$;

			//自定义验证规则
			form.verify({
				title: function (value) {
					if (value.length < 5) {
						return '账号至少得5个字符啊';
					}
				}
				, pass: [
					/^[\S]{6,12}$/
					, '密码必须6到12位，且不能出现空格'
				]
			});

			//监听提交
			form.on('submit(demo1)', function (data) {

				if (localStorage.type < 20) {
					alert("权限不足已记录当前登录ip，请刷新页面后重试");
					return;
				}

				// base64 加密
				var encryptPass = window.btoa(data.field.password);
				encryptPass = hex_md5(encryptPass + KEY);

				var user = {
					userName: data.field.username,
					password: encryptPass
				};
				user = JSON.stringify(user);
				// console.log(user);

				$.ajax({
					url: "/api/READ/api/my/login/loginc",
					type: "POST",
					traditional: true,//这使json格式的字符不会被转码
					contentType: 'application/json; charset=UTF-8',
					datatype: "json",
					data: user,
					success: function (res) {
						if (res.code === '200') {
							var data = res.data;
							// console.log(data)

							if (data.type < 20) {
								alert("权限不足已记录当前登录ip，请刷新页面后重试");
								localStorage.type = data.type;
								return;
							}

							layer.alert("登录成功,1s后跳转...", {
								title: '提示'
							});

							setTimeout(() => {
								layer.closeAll();
								// 保存token && 跳转页面
								// 保存过期时间 2h	2*60*60*1000
								localStorage.outtime = Date.parse(new Date()) + 2 * 60 * 60 * 1000;
								localStorage.token = data.token;
								localStorage.name = data.userName;
								localStorage.uid = data.userId;
								localStorage.type = data.type;
								window.location = './index.html';
							}, 1000);

						} else {
							layer.alert(res.msg, {
								title: '登录失败'
							});
							return false;
						}
					},
					error: function (res) {
						layer.alert("服务器正在更新，暂不提供服务", {
							title: '提示'
						});
					}
				});
				return false;
			});

		});
	</script>

</body>

</html>